<template>
  <div class="search-container" v-loading="loading">
    <el-form v-model="searchPage" inline="true" >
      <el-form-item  v-for="(item, index) in searchPage.items" :key="index" :label="item.cnname">
        <el-select  v-if="item.itemType === 'DICT'" v-model="item.value" :multiple = "item.mutSelect ==='checkBox'">
          <el-option  v-for="(itemChild, itemIndex) in item.dict" :key="itemIndex"  :label="itemChild.value" :value="itemChild.key" />
        </el-select>
        <el-input v-else-if="item.itemType !== 'DATE'" v-model="item.value"></el-input>
        <el-col :span="24" v-else>
          <el-form-item>
            <el-date-picker
              v-model="item.dateValue"
              type="date"
              label="Pick a date"
              placeholder="Pick a date"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button type="primary" @click="onCreate">新建</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="data-container">
      <el-table :data="tableData" style="width: 100%">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" width="80" label="序号"/>
      <el-table-column prop="type" label="类别" width="100">
        <template #default="scope">
          <el-select v-model="scope.row.type" placeholder="" name="type" disabled >
            <el-option value="1" label="门诊" />
            <el-option value="2" label="住院" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="regNum" label="登记号" width="160" />
      <el-table-column prop="name" label="姓名" width="120" />
      <el-table-column prop="sex" label="性别" width="120" >
        <template #default="scope">
          <el-select v-model="scope.row.sex" placeholder="" name="sex" disabled >
            <el-option value="1" label="男" />
            <el-option value="2" label="女" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="birthday" label="出生日期" width="160" />
      <el-table-column prop="age" label="年龄" width="80" />
      <el-table-column prop="phone" label="手机号码" width="200"></el-table-column>
      <el-table-column prop="memo" label="备注" width="200" />
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column fixed="right" label="操作" width="140">
        <!-- vue3中 solt-scop已经放弃使用 -->
        <template #default="scope">
           <el-button text type="danger" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
           <el-button text type="primary" size="small" @click="handleEdit(scope.row.id)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
     <el-pagination
      v-model:currentPage="searchPage.current"
      v-model:page-size="searchPage.pageSize"
      :page-sizes="[20, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableDatas.total"
      hide-on-single-page=true
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  <PatientForm :isShow="dialogVisible" @diaCancel="diaCnacel" :id="currentId"></PatientForm>
</template>

<script setup>
import { ref, onActivated } from 'vue'
import { ElMessage } from 'element-plus'
import { list, deleteById, downLoadFile, add, modify } from '@/api/common/common'
import { dateToString, dateToNext } from '@/utils/date'
import PatientForm from '../form/index.vue'
const tableDatas = ref({})

const tableData = ref([])

const dialogVisible = ref(false)

const currentId = ref(null)

const searchPage = ref({
  items: [
    {
      name: 'regNum',
      cnname: '登记号',
      itemType: 'STRING',
      sign: '==',
      value: '',
      dateValue: ''
    },
    {
      name: 'name',
      cnname: '名称',
      itemType: 'STRING',
      sign: 'like',
      value: '',
      dateValue: ''
    },
    {
      name: 'createTime1',
      cnname: '开始时间',
      itemType: 'DATE',
      sign: '>=',
      value: dateToString(-7),
      dateValue: dateToNext(-7)
    },
    {
      name: 'createTime2',
      cnname: '结束时间',
      itemType: 'DATE',
      sign: '<=',
      value: dateToString(0),
      dateValue: dateToNext(0)
    }
  ],
  pageSize: 20,
  current: 1,
  sortBy: ['id']
})

const searchFunction = async () => {
  list(searchPage.value, 'grk/patient').then(res => {
    tableData.value = res.content
    tableDatas.value.total = parseInt(res.totalElements)
    ElMessage.success('成功')
  }).catch(err => {
    ElMessage.success('失败' || err.message)
  })
}

onActivated(searchFunction())

const onSubmit = () => {
  searchFunction()
}

const onReset = () => {
  searchPage.value = {
    items: [
      {
        name: 'name',
        cnname: '名称',
        itemType: 'STRING',
        sign: 'like',
        value: '',
        dateValue: ''
      },
      {
        name: 'createTime1',
        cnname: '开始时间',
        itemType: 'DATE',
        sign: '>=',
        value: dateToString(-7),
        dateValue: dateToNext(-7)
      },
      {
        name: 'createTime2',
        cnname: '结束时间',
        itemType: 'DATE',
        sign: '<=',
        value: dateToString(0),
        dateValue: dateToNext(0)
      }
    ],
    pageSize: 20,
    current: 1,
    sortBy: ['id']
  }
  // 处理查询
  searchFunction()
}

const handleSizeChange = pageSize => {
  searchPage.value.pageSize = pageSize
  // 执行查询操作
  searchFunction()
}

const handleCurrentChange = current => {
  searchPage.value.current = current
  // 执行查询操作
  searchFunction()
}

const handleDelete = id => {
  deleteById(id, 'grk/patient').then(res => {
    ElMessage.warning('删除成功')
    // 重新刷新
    searchFunction()
  })
}

const handleEdit = id => {
  // ElMessage.success('编辑')
  dialogVisible.value = true
  currentId.value = id
}

const onCreate = () => {
  dialogVisible.value = true
  currentId.value = null
}

const diaCnacel = disable => {
  dialogVisible.value = false
  searchFunction()
  currentId.value = null
}
</script>

<style>

</style>
